﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http.Filters;

using ProjectBase.Core;
using ProjectBase.Core.Dao;
using ProjectBase.Core.Service;

namespace $safeprojectname$
{
    public class ExceptionHandlerAttribute : ExceptionFilterAttribute
    {
        #region IExceptionFilter Members
        public override void OnException(HttpActionExecutedContext actionExecutedContext)
        {
            NHSessionRollback();
            DbSessionRollback();
            XmlSessionRollback();
            base.OnException(actionExecutedContext);
        }
        #endregion

        // NHibernate
        protected virtual void NHSessionRollback()
        {
            if (NHSession.TransactionIsActive)
            {
                NHSession.TransactionRollback();
            }
        }

        // DbSession
        protected virtual void DbSessionRollback()
        {
            if (DbSession.TransactionIsActive)
            {
                DbSession.TransactionRollback();
            }
        }

        // XmlSession
        protected virtual void XmlSessionRollback()
        {
            if (XmlSession.TransactionIsActive)
            {
                XmlSession.TransactionRollback();
            }
        }

        #region session providers
        INHSessionHandler _nhSession;
        protected virtual INHSessionHandler NHSession
        {
            get { return _nhSession ?? (_nhSession = Factory.CreateInstance<INHSessionHandler>()); }
        }
        IDbSessionHandler _dbSession;
        protected virtual IDbSessionHandler DbSession
        {
            get { return _dbSession ?? (_dbSession = Factory.CreateInstance<IDbSessionHandler>()); }
        }
        IXmlSessionHandler _xmlSession;
        protected virtual IXmlSessionHandler XmlSession
        {
            get { return _xmlSession ?? (_xmlSession = Factory.CreateInstance<IXmlSessionHandler>()); }
        }
        #endregion session providers


    }
}